<?php
if (!extension_loaded('php-gtk')) {
  exit('The PHP-Gtk2 module is not available!' . PHP_EOL);
}

class Mine extends GtkToggleButton
{
  var $x;
  var $y;
  var $content = false;
}

class MineMine{
  var $rows = 5;
  var $columns = 10;
  var $mines = 10;
  var $glade;
  var $main_window;
  var $g_table;
  var $g_statusbar;

  var $minefield;
  var $traps;
  var $timeout_handler_id;
  var $exploded = false;
  var $tick = 0; // time (second)

  function __construct() {
    $this->glade = new GladeXML(dirname(__FILE__) . '/minemine.glade');
    $this->glade->signal_autoconnect_instance($this);

    $this->g_statusbar = $this->glade->get_widget('statusbar1');
    $this->timeout_handler_id = Gtk::timeout_add(1000, array($this, 'onTimeout'));
echo 'd';
    $this->minefield = $this->glade->get_widget('table1');
    echo 'd';
    $this->create_minefield($this->columns, $this->rows, $this->mines);
echo 'd';
    $this->main_window = $this->glade->get_widget('window1');
    $this->main_window->show_all();

  }

  function addMessage($string){
    $context_id = $this->g_statusbar->get_context_id('example');
    $this->g_statusbar->push($context_id, $string);
  }
  function create_minefield($columns, $rows, $mines){
    $this->minefield->resize($rows, $columns);
    for ($y = 0; $y < $rows; $y++){
      for ($x = 0; $x < $columns; $x++){
        $idx =$y*$columns+$x;
        $trap = new Mine( "$x,$y,$idx" );
        $trap->set_focus_on_click(false);
        //$trap->set_size_request(24,24);
        $trap->x = $x;
        $trap->y = $y;
        $trap->set_events(Gdk::BUTTON_RELEASE_MASK);
        $trap->connect('button-release-event', array($this, 'stepOn'), $trap);
        $trap->connect('toggled', array($this, 'dontFlip'));
        $this->minefield->attach($trap, $x, $x+1, $y, $y+1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
      }
    }
    $this->resetMinefield($columns, $rows, $mines);

    return;
  }
  function resetMinefield($columns, $rows, $mines){
    $this->traps = $this->minefield->get_children();

    for ($i = 0; $i < $mines; $i++){
      $index = rand(0, $rows * $columns - 1);
      var_export($index);
      var_export($this->traps[$index]->content);

      if ( $this->traps[$index]->content ) {
        $i--;
        continue;
      } else {
        $this->traps[$index]->content = true;
      }
    }
  }
  function isMine($x, $y){
    if ( $x >= 0 and $x < $this->columns and $y >= 0 and $y < $this->rows){
      $idx = $x + $y * $this->columns;
      if ($this->traps[$idx]->content === true){
        return true;
      }
    }
    return false;
  }
  function checkAround($x, $y){
    $around = 0;
    // 8-way
    if ($this->isMine($x-1, $y-1)){
      $around++;
    }
    if ($this->isMine($x-1, $y)){
      $around++;
    }
    if ($this->isMine($x-1, $y+1)){
      $around++;
    }
    if ($this->isMine($x, $y-1)){
      $around++;
    }
    if ($this->isMine($x, $y+1)){
      $around++;
    }
    if ($this->isMine($x+1, $y-1)){
      $around++;
    }
    if ($this->isMine($x+1, $y)){
      $around++;
    }
    if ($this->isMine($x+1, $y+1)){
      $around++;
    }
    return $around;
  }
  function stepOn(GtkWidget $widget, GdkEvent $event, GtkButton $button){
    if ($button->get_active()){
      return;
    }
    switch ($event->button) {
      case 1: // LEFT_BUTTON
        echo 'left' . PHP_EOL;
        if ($button->content === true ){
          $button->set_label('*');
          $this->setGameOver();
        } else {
          $button->set_label($this->checkAround($button->x, $button->y));
        }
        break;
      case 2: // MIDDLE_BUTTON
        echo 'mid' . PHP_EOL;
        break;
      case 3: // RIGHT_BUTTON
        echo 'right' . PHP_EOL;
        break;
    }
  }
  function dontFlip(GtkButton $button){
    if ($button->get_active()){

    } else {
      $button->set_active(true);
      return;
    }
    //$this->addMessage($event->button . ' - ' . $event->x . ':' . $event->y . "x$x y$y");
    //$button->set_active(true);



    return;
  }
  function setGameOver(){
    Gtk::timeout_remove($this->timeout_handler_id);

    $dialog = new GtkMessageDialog(
      $this->main_window,
      Gtk::DIALOG_DESTROY_WITH_PARENT,
      Gtk::MESSAGE_QUESTION,
      Gtk::BUTTONS_YES_NO,
      'Oops! You exploded! Do you like play again?');
    $dialog->set_title('Game over');
    $response_id = $dialog->run();
    $dialog->destroy();
    if ($response_id == Gtk::RESPONSE_YES) {
        echo 'YES' . PHP_EOL;
    } else {
        echo $response_id . PHP_EOL;
    }
  }
  function onTimeout(){
    if ($this->exploded){
      return false;
    }
    $this->tick++;
    $this->addMessage('Time: ' . $this->tick);
    return true;
  }
  function about(){
    $g_aboutdialog = $this->glade->get_widget('aboutdialog1');
    $g_aboutdialog->run();
  }
  function closeAbout($gd){
    $gd->hide();
  }
}





//Start the main loop
new MineMine();
Gtk::main();
?>